<?php
namespace Tyrone\Command;
use Medoo\Medoo;
use Tyrone\Model\Game\LogLogin;
use Swoole\Coroutine;
use Tyrone\Model\Game\PayLog;

class PlayerKeep extends Base
{

    public function divideHandle($mysql,$platform){
        //中央服跑一次
        $datas = [];
        $database = $this->database;
        $game_database = $this->game_mysql;
        $day = date("Ymd",strtotime($this->sDate));
        $eDate = date("Ymd",strtotime($this->eDate));

        $PlayerKeepModel = new \Tyrone\Model\PlayerKeep($mysql);
        $PlayerOnlineModel = new \Tyrone\Model\PlayerOnline($mysql);

        $keepDays = range(2, 90);
        $datas = [];
        while ($day <= $eDate) {
            $alias = [
                'day' => Medoo::raw($day),
            ];
            $where = [
                'b.platform' => $platform,
            ];
            $where2 = [
                'platform' => $platform,
            ];
            foreach ($keepDays as $keep) {
                $regDay = date("Ymd", strtotime($day) - ($keep - 1) * 86400);
                $alias['keep_day'] = Medoo::raw(intval((strtotime($day) - strtotime($regDay))/86400));
                //留存数
                $loginKeep = $PlayerOnlineModel->playerLoginKeep($day, $regDay,$where,array_merge(['type'=>Medoo::raw('1')],$alias));
                $payKeep = $PlayerOnlineModel->playerPayKeep($day, $regDay,$where2,array_merge(['type'=>Medoo::raw('2')],$alias));
                //regDay 注册数
                $alias['keep_day'] = Medoo::raw('1');//0不支持
                $register = $PlayerOnlineModel->playerRegister($regDay,$where2,array_merge(['type'=>Medoo::raw('1')],$alias));
                $pay = $PlayerOnlineModel->playerPay($regDay,$where2,array_merge(['type'=>Medoo::raw('2')],$alias));

                $datas = array_merge($datas,$loginKeep);
                $datas = array_merge($datas,$payKeep);
                $datas = array_merge($datas,$register);
                $datas = array_merge($datas,$pay);
            }
            //日期+1
            $day = date('Ymd', strtotime($day) + 86400);
        }
        //聚合
        $uniqueKey = ['day','type','platform','plat_server'];
        $list = [];
        foreach ($datas as $item){
            $keyData = array_intersect_key($item,array_flip($uniqueKey));
            $key = implode('_',$keyData);
            if(!isset($list[$key])){
                $list[$key] = $keyData;
                $list[$key]['info'] = [];
            }else{
                $list[$key]['info'] = unserialize($list[$key]['info']);
            }
            $list[$key]['info'][$item['keep_day']] = $item['player_num'];
            //兼容
            if($item['keep_day']==1){
                $list[$key]['info'][0] = $item['player_num'];
            }
            $list[$key]['info'] = serialize($list[$key]['info']);

        }
        foreach (array_chunk($list,1000) as $items){
            $PlayerKeepModel->updateData($items);
            usleep(100);
        }
        return $this->result([count($list)]);
    }


    public  function execute()
    {
        return $this->divideCenter();


    }
}